package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;

import com.lb.jwitter.service.TblDKMHHocVienMonHocDuocDKService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblTaiKhoanService;
import com.lb.jwitter.service.TblThongTinHocVien;
import com.lb.jwitter.service.TblThongTinHocVienMonHoc;
import com.lb.jwitter.service.TblThongTinHocVienMonHocService;
import com.lb.jwitter.service.TblThongTinHocVienService;

import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.ExcelToJSon;
import com.lb.jwitter.util.ExcelToJSon2;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class TblDKMHHocVienMonHocDuocDKSaveFromXLSAction {


	private TblTaiKhoan taiKhoanDangNhap; 
	  
	  public TblTaiKhoan getTaiKhoanDangNhap() { 
	      return taiKhoanDangNhap; 
	  } 
	  public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
	      this.taiKhoanDangNhap = taiKhoanDangNhap; 
	  } 
	  
	  private int dotDKId;
	  
	public int getDotDKId() {
		return dotDKId;
	}
	public void setDotDKId(int dotDKId) {
		this.dotDKId = dotDKId;
	}

	@Autowired // Injected by Spring 
	ApplicationContextProvider contextProvider; 
	 	 
	private Message msg;
	
	  public Message getMsg() {
		return msg;
	}

	public void setMsg(Message msg) {
		this.msg = msg;
	}
	
	private JSONArray jsons;

	public JSONArray getJsons() {
		return jsons;
	}
	public void setJsons(JSONArray jsons) {
		this.jsons = jsons;
	}
	
	private String destFile;
	public String getDestFile() {
		return destFile;
	}
	public void setDestFile(String destFile) {
		this.destFile = destFile;
	}
	
	public String execute() throws Exception
	{


	     try{ 	
	    	 
	         Map session = ActionContext.getContext().getSession();
	         taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
	         if( taiKhoanDangNhap == null) return "noprivilege";
	         
	        ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 

	        TblDKMHHocVienMonHocDuocDKService tblDKMHHocVienMonHocDuocDKService 
	        	= (TblDKMHHocVienMonHocDuocDKService) context.getBean("tbl_DKMH_HocVien_MonHoc_DuocDKClient"); 

	        //get json from excel
	        if(ExcelToJSon2.getExtension(destFile).equals("XLSX")){
	        	jsons = ExcelToJSon2.execute(destFile);
	        	System.out.println("EXCEL 2007: " + destFile);
	        }else{
	        	jsons = ExcelToJSon.execute(destFile);
	        	System.out.println("EXCEL 2003: " + destFile);
	        }
	        
	        tblDKMHHocVienMonHocDuocDKService.createListtblDKMHHocVienMonHocDuocDKByJSON(jsons.toString());
	 
	        
	            return Action.SUCCESS; 
	     } 
	     catch(Exception e){ 
	             System.out.println("Loi add ky hieu from excell Action"); 
	             e.printStackTrace(); 
	             return Action.ERROR; 
	     } 

	}
	
	
	
	
	
}
